package self.Lunastic.HexaSweeper.Unit;

import self.Lunastic.HexaSweeper.Manager.*;
import self.Lunastic.HexaSweeper.Object.GameCeil;

import java.awt.*;

/**
 * 格子按钮
 */
public class ButtonCeil extends ButtonBase {
    // 踩到炸弹
    private boolean _clickBomber = false;

    // 对应格子数据
    public GameCeil ceil;
    /**
     * 构造
     * @param link 对应格子
     */
    public ButtonCeil(GameCeil link){
        super(0, 0, 0, 0);
        this.ceil = link;link.buttonCeil = this;
        this.setBackground(StyleOption.BUTTON_COVER);
        this.endUpdate(); // 格子不需要更新
    }

    @Override
    protected void extraPaint() {
        this.drawBackground();
        if(this.ceil.isCover()){
            if(this.ceil.isFlag() && !GameManager.isGamePause()) this.drawFlag();
        }else{
            if(this.ceil.getType() > 0 && !GameManager.isGamePause()) this.drawNumber();
        }
    }

    // 根据状态获取背景颜色
    private Color getBackgroundColor(){
        if (GameManager.isGameStart() && GameManager.isGamePause()) return StyleOption.CEIL_PAUSE;
        if(this._clickBomber) return StyleOption.CEIL_BOMBER_CLICK;
        if(this.ceil.isCover()){
            if(this.ceil.isFlag()) {
                // 被标记
                this.drawFlag();
                return StyleOption.CEIL_COVER;
            }
            else if(GameManager.isGameFailed() && this.ceil.getType() == -1) return StyleOption.CEIL_BOMBER_COVER;
            // 被点击（包括邻格点击
            else if(this.isClick()){ return StyleOption.CEIL_CLICK; }
            // 被悬停
            else if(this._hover){ return StyleOption.CEIL_HOVER; }

            return StyleOption.CEIL_COVER;
        }else {
            return StyleOption.CEIL_DISCOVER;
        }
    }

    // 绘制背景
    private void drawBackground(){
        g2d.setColor(this.getBackgroundColor());
        // xy -> 中心点
        int x = this.ceil.r;
        int y = this.ceil.r;
        int r = (int) (this.ceil.r * 0.95);
        int[] xPs = new  int[] {x,x+r,x+r,x,x-r,x-r};
        int[] yPs = new  int[] {
                y-(int)(1.1547*r),
                y-(int)(0.57735*r),
                y+(int)(0.57735*r),
                y+(int)(1.1547*r),
                y+(int)(0.57735*r),
                y-(int)(0.57735*r),
        };
        int nPs = 6;
        g2d.fillPolygon(xPs, yPs, nPs);
    }
    // 绘制标记
    private void drawFlag(){
        int r = this.ceil.r;
        // 旗杆
        g2d.setColor(StyleOption.FLAG_POST);
        g2d.fillRect((int)(r*0.75), (int)(r*0.5), (int)(r*0.25), r);
        // 旗面
        g2d.setColor(StyleOption.FLAG_FACE);
        int[] xPs = new int[] {r,(int)(r*1.5),r};
        int[] yPs = new int[] {(int)(r*0.5),r,r};
        int   nPs = 3;
        g2d.fillPolygon(xPs, yPs, nPs);
        // 错误旗帜标记
        if(GameManager.isGameFailed() && this.ceil.getType() > -1) {
            g2d.setColor(StyleOption.FLAG_ERROR);
            xPs = new int[]{(int)(r*0.5), (int) (r * 1.75), (int) (r * 1.5), (int)(r*0.25)};
            yPs = new int[]{(int)(r * 0.25), (int)(r * 1.5), (int)(r * 1.75), (int)(r * 0.5)};
            nPs = 4;
            g2d.fillPolygon(xPs, yPs, nPs);
        }
    }

    // 绘制数字
    private void drawNumber(){
        g2d.setColor(StyleOption.CEIL_NUMBER[this.ceil.getType()]);
        g2d.setFont(new Font(StyleOption.CEIL_FONT, Font.BOLD, this.ceil.r));
        g2d.drawString(""+this.ceil.getType(), this.ceil.r*2/3, this.ceil.r*4/3);
    }

    // 左键事件
    @Override
    protected void doLeftReleased(){
        if(this.ceil.isFlag()) return; // 被标记时无效果
        if (this.ceil.isCover()) if(this.ceil.getType() == -1){
            this._clickBomber = true;
            GameManager.failed();

        }else {
            // 第一次点击时 启动游戏
            if (GameManager.isGameReset()){
                GameManager.start();
                GameManager.$gameArea.createBomber(this.ceil.x, this.ceil.y);
            }
            this.ceil.discover();
            GameManager.coverCount --;
            if (this.ceil.getType() == 0) this.doZeroClick();
            if (GameManager.coverCount <= 0){ GameManager.succeed(); }
        }
    }
    // 右键事件
    @Override
    protected void doRightReleased(){
        if(this.ceil.isCover())
            if (this.ceil.isFlag()){
                this.ceil.removeFlag();
                GameManager.flagCount ++;
            }else {
                this.ceil.setFlag();
                GameManager.flagCount --;
            }
    }
    // 双键事件
    @Override
    protected void doDoubleReleased(){

        if(!this.ceil.isCover()){
            int flagCount = 0;
            for (GameCeil ac:this.ceil.aroundCeil) {
                if(ac.isCeil() && ac.isFlag()) flagCount ++;
            }
            if(flagCount == this.ceil.getType())
                for (GameCeil ac:this.ceil.aroundCeil) {
                    // 尝试点击临格
                    if(ac.isCeil()) ac.buttonCeil.doLeftReleased();
                }
        }
        this.doDoubleReleaseEffect();
    }

    // 周围按下特效
    @Override
    protected void doDoublePressed(){
        if(!this.ceil.isCover()) for (GameCeil ac:this.ceil.aroundCeil) {
            if(ac.isCeil() && !ac.isFlag() && ac.isCover()) ac.press();
        }
    }

    // 周围抬起特效特效
    private void doDoubleReleaseEffect(){
        for (GameCeil ac:this.ceil.aroundCeil) {
            if(ac.isCeil())ac.release();
        }
    }

    // 重置格子交互状态
    @Override
    protected void doExited(){
        super.doExited();
        ceil.release();
        this.doDoubleReleaseEffect();
    }

    // 是否被点击
    private boolean isClick(){
        return this.ceil.isClick() || this._leftPress;
    }

    // 处理 0 格子点击
    private void doZeroClick(){
        for (GameCeil ac:this.ceil.aroundCeil) {
            if(ac.isCeil() && !ac.isFlag() && ac.isCover()) {
                ac.buttonCeil.doLeftReleased();
                ac.buttonCeil.repaint();
            }
        }
    }
}
